<template>
  <div class="app-container">
    <el-card>
      <div v-if="showSearch" slot="header" class="clearfix">
        <!--头部工具过滤-->
        <el-input v-model="listQuery.name" size="mini" maxlength="255" clearable style="width: 200px;margin-right: 5px;" placeholder="请输入岗位名称" />
        <el-button type="success" size="mini" icon="el-icon-search" @click="handleFilter">搜索</el-button>
        <el-button type="warning" size="mini" icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
      </div>
      <div class="opts">
        <!--左侧-->
        <el-button type="success" :disabled="hasEdiObj" icon="el-icon-edit" size="mini" @click="handleUpdate(selectionChange[0])">同步</el-button>
        <!--右侧-->
        <right-toolbar :show-search.sync="showSearch" :columns="columns" @queryTable="resetForm" />
      </div>
      <!--表格数据-->
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%;"
        row-key="id"
        lazy
        size="mini"
        element-loading-text="正在努力加载中..."
        :load="load"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        highlight-current-row
        border
        fit
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns[0].visible" prop="tableName" label="表名" />
        <el-table-column v-if="columns[1].visible" prop="engine" label="数据库引擎">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.enabled" active-color="#13ce66" inactive-color="#ff4949" @change="updateEnabled({id: scope.row.id,enabled: scope.row.enabled},scope.row)" />
          </template>
        </el-table-column>
        <el-table-column v-if="columns[2].visible" prop="tableCollation" label="字符集">
          <template scope="scope">
            {{ scope.row.createdUser?scope.row.createdUser.name:'' }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns[3].visible" prop="createTime" label="创建时间" />
        <el-table-column v-if="columns[4].visible" prop="tableComment" label="备注" />
        <el-table-column width="200px" label="操作">
          <template slot-scope="scope" />
        </el-table-column>
      </el-table>
      <!--分页-->
      <pagination v-show="total>0" :total="total" :page.sync="page.pageNum" :limit.sync="page.pageSize" @pagination="queryAll" />
    </el-card>
  </div>
</template>

<script>
import RightToolbar from '@/components/RightToolbar'
import Pagination from '@/components/Pagination'
import { findAll, findByPid } from '@/api/job'
export default {
  name: 'Gen',
  components: { Pagination, RightToolbar },
  data() {
    return {
      showSearch: true,
      listQuery: {
        pid: 0,
        name: undefined
      },
      selectionChange: null,
      total: 0,
      ids: [],
      page: {
        pageNum: 1,
        pageSize: 10,
        sort: '+sort'
      },
      hasDelObj: true,
      hasEdiObj: true,
      columns: [
        { key: 0, label: `部门名称`, visible: true },
        { key: 1, label: `排序`, visible: true },
        { key: 2, label: `是否启用`, visible: true },
        { key: 3, label: `创建人`, visible: true },
        { key: 4, label: `创建时间`, visible: true }
      ],
      tableData: [],
      loading: false
    }
  },
  created() {
    this.queryAll()
  },
  methods: {
    handleFilter() {
      this.page.pageNum = 1
      this.queryAll()
    },
    queryAll: function() {
      this.loading = true
      this.tableData = []
      findAll(this.page, this.listQuery).then(res => {
        this.tableData = res.list
        this.total = res.total
        setTimeout(() => { this.loading = false }, 500)
      }).catch(() => {
        this.loading = false
      })
    },
    resetForm() {
      this.listQuery = { pid: 0, name: undefined }
      this.handleFilter()
    },
    load(tree, treeNode, resolve) {
      setTimeout(() => {
        findByPid(tree.id).then(res => {
          resolve(res)
        })
      }, 1000)
    },
    handleSelectionChange(val) {
      this.selectionChange = val
      this.ids.length = 0
      const _this = this
      if (val.length > 0) {
        this.hasEdiObj = val.length !== 1
        this.hasDelObj = false
      } else {
        this.hasDelObj = true
        this.hasEdiObj = true
      }
      val.forEach((element) => {
        _this.ids.push(element.id)
      })
    }
  }
}
</script>

<style scoped>
.opts {
  padding: 4px 0;
  display: -webkit-flex;
  display: flex;
  align-items: center;
}
.opts .opts-right {
  margin-left: auto;
}
</style>
